package com.gyf.controller;

import com.gyf.pojo.Books;
import com.gyf.services.BookBizImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author GYF
 * @date 2020/5/13
 **/
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("bookBiz")
    private BookBizImpl bookBiz;

    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> list = bookBiz.queryAllBook();
        model.addAttribute("list", list);
        return "allBook";
    }

    @RequestMapping("/toAddBook")
    public String addBook(){
        return "addBook";
    }

    @RequestMapping("/addBook")
    public String addPaper(Books books) {
        System.out.println(books);
       bookBiz.addBook(books);
        //重定向
        return "redirect:/book/allBook";
    }

    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(Model model, Integer bookID) {
        Books books = bookBiz.queryBookById(bookID);
        System.out.println(books);
        model.addAttribute("book",books );
        return "updateBook";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Model model, Books book) {
        System.out.println(book);
        bookBiz.updateBook(book);
        Books books = bookBiz.queryBookById(book.getBookID());
        model.addAttribute("books", books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") Integer id) {
        bookBiz.deleteBookById(id);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/Inquire")
    public String InquireBookName(String bookName, Model model){
        List<Books> books = bookBiz.findName(bookName);
        System.out.println(books);
        if(books.size()==0){
            System.out.println("没有");
            books = bookBiz.queryAllBook();
            model.addAttribute("error","没有查询到");
        }
        model.addAttribute("list",books);
        return "allBook";
    }
}

